package me.lc.jokealot;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.mobclick.android.MobclickAgent;

public class CategoryActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.category_view);
		setUI();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	private void setUI() {
		ListView lv = (ListView) this.findViewById(R.id.lstJokes);
		JokeListAdapter da = new JokeListAdapter();
		lv.setAdapter(da);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(CategoryActivity.this,
						JokeActivity.class);
				intent.putExtra("index",
						DataManager.instance.currentJokeItemList.get(arg2)
								.getIndex());
				CategoryActivity.this.startActivity(intent);
				CategoryActivity.this.finish();
			}
		});

		TextView txtCat = (TextView) findViewById(R.id.txtCategoryName);
		txtCat.setText(DataManager.instance.currentCategoryName);
	}

	class JokeListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return DataManager.instance.currentJokeItemList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View item = CategoryActivity.this.getLayoutInflater().inflate(
					R.layout.joke_item, null);
			TextView txt = (TextView) item.findViewById(R.id.jokeName);
			txt.setText(DataManager.instance.currentJokeItemList.get(position)
					.getTitle());
			return item;
		}

	}
}
